#include "stm32f10x.h"
#include "rtc.h"
#include "key.h"
#include "lcd.h"
#include "stdio.h"

u32 TimingDelay = 0;
u8 shrink_flag = 1;
u16 led_count = led5;
extern u8 shrink_flag;
extern u8 lift_state;
extern u8 now_floor;

//Main Body
int main(void)
{
	u32 time = 0;
	u16 led = 0;
	char text[20];
	SysTick_Config(SystemCoreClock/1000);
	STM3210B_LCD_Init();
  LCD_SetTextColor(White);
  LCD_SetBackColor(Blue2);
  LCD_Clear(Blue2);
	key_init();
	rtc_init();
	tim2_init();
	pwm_init();
	led_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	while(1)
	{
		time = RTC_GetCounter();
		deal_key();
		deal_state();

		if(lift_state == state_lift_arrive_aim){
			sprintf(text,"      Now Stage  ");
			LCD_DisplayStringLine(Line2,text);
			if(shrink_flag == 1){
				sprintf(text,"         %d       ",now_floor+1);
				LCD_DisplayStringLine(Line4,text);
			}else if(shrink_flag == 0){
				LCD_ClearLine(Line4);
			}
			sprintf(text,"       %d:%d:%d      ",time/3600,(time%3600)/60,time%60);
			LCD_DisplayStringLine(Line6,text);
		}
		else if(lift_state != state_lift_arrive_aim){
			sprintf(text,"      Now Stage  ");
			LCD_DisplayStringLine(Line2,text);
			sprintf(text,"         %d       ",now_floor+1);
			LCD_DisplayStringLine(Line4,text);
			sprintf(text,"       %d:%d:%d      ",time/3600,(time%3600)/60,time%60);
			LCD_DisplayStringLine(Line6,text);
		}
	}
}

